//
//  LoginView.swift
//  ShouChong
//
//  Created by yw on 2024/9/21.
//

import SwiftUI
import MMDrawerController

struct LoginView: View {
    @Environment(\.presentationMode) var mode
    @State private var account:String = ""
    @State private var password:String = ""
    @AppStorage("login_token") var loginToken:String = ""
    @ObservedObject var vm:WelcomeVM = WelcomeVM()
    var body: some View {
        ScrollView {
            HStack {
                Spacer()
                ShakeButton(label:
                                Image(systemName: "xmark")
                    .foregroundColor(Color("color_gray"))
                            
                ) {
                    mode.wrappedValue.dismiss()
                }.modifier(MainTextModifier(fontWeight: .bold))
                    .padding(.horizontal,20)
                    .padding(.vertical,10)
            }
            .padding(.top,10)
            .padding(.bottom,20)
            bindView
            
            
        }
        .background(Color("color_bg"))
    }
    
    

    
    var bindView:some View {
        VStack {
            HStack {
                Text("输入您的账号，长度不少于6位，账号无法修改⚠️")
                    .modifier(MainTextModifier(fontSize: 12, fontWeight: .regular, foreColor: Color("color_gray")))
                Spacer()
            }
            .padding(.horizontal,10)
            .padding(.bottom,4)
            
            TextField("建议输入QQ号/邮箱", text: $account)
                .padding(.horizontal,10)
                .frame(width: kScreenWidth - 20,height: 56)
                .background(Color("color_white"))
                .cornerRadius(10)
                .padding(.bottom,20)
                    
            
            
            HStack {
                Text("输入您账号的密码，长度不少于6位")
                    .modifier(MainTextModifier(fontSize: 12, fontWeight: .regular, foreColor: Color("color_gray")))
                Spacer()
            }
            .padding(.horizontal,10)
            .padding(.bottom,4)
            
            SecureField("请输入密码", text: $password)
                .padding(.horizontal,10)
                .frame(width: kScreenWidth - 20,height: 56)
                .background(Color("color_white"))
                .cornerRadius(10)
                .padding(.bottom,40)
                
            
            ShakeButton(label: Text("确 定").frame(width: kScreenWidth - 50)) {
                UIApplication.shared.endEditing()
                if account.count < 6 {
                    YWToast.show(text: "账号长度过短，建议6位以上")
                    return
                }
                if password.count < 6 {
                    YWToast.show(text: "密码长度过短，建议6位以上")
                    return
                }
                vm.login(account: account, password: password) {token in 
                    loginToken = token
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                        let vc = BaseNavController(rootViewController: HomeViewController())
                        let settingVC = OpenNavViewController(rootViewController: SettingViewController())
                        let mmVC = MMDrawerController(center: vc, leftDrawerViewController:settingVC)
                        mmVC?.openDrawerGestureModeMask = .all
                        mmVC?.closeDrawerGestureModeMask = .all
                        mmVC?.showsShadow = true
                        mmVC?.maximumLeftDrawerWidth = 200
                        mmVC?.shadowRadius = 5
                        mmVC?.shadowColor = .gray.withAlphaComponent(0.4)
                        mmVC?.shouldStretchDrawer = false
                        UIApplication.shared.keyWindow?.rootViewController = mmVC
                    }
                }

            }.modifier(MainButtonModifier(width: kScreenWidth - 50))
                
        }
        
        
    }
}

